#include <my_header.h>
#include "cmd.h"
/* Usage:  */
int pwd(MYSQL *db, my_stack *pstack, char *buf, size_t bufsize){                                  
    /*MYSQL *db = mysql_init(NULL);
    char *host = "localhost";
    char *user = "root";
    char *password = "root";
    char *database = "zwy";
    MYSQL *ret = mysql_real_connect(db, host, user, password, database, 0, NULL, 0);
    if(ret == NULL){
        printf("Error:%s\n", mysql_error(db));
        exit(0);
    }*/
    int cur_id = pstack->stack_top->id;
    while(1){
        char sql[256];
        snprintf(sql, sizeof(sql), "select parent_id, type, path from virtual_directory where id = %d", cur_id);
        int qret = mysql_query(db, sql);
        if(qret != 0){
            snprintf(buf, bufsize, "Error:%s", mysql_error(db));
            return -1;
        }
        MYSQL_RES *result = mysql_store_result(db);
        if(!result){
            snprintf(buf, bufsize, "result error:%s", mysql_error(db));
            return -1;
        }
        MYSQL_ROW row = mysql_fetch_row(result);
        if(!row){
            snprintf(buf, bufsize, "No such ID:%d", cur_id);
            mysql_free_result(result);
            return -1;
        }
        const char *parent_id = row[0];
        const char *type = row[1];
        const char *path = row[2];
        if(type && strcmp(type, "d") == 0){
            strncpy(buf, path, bufsize - 1);
            buf[bufsize - 1] = '\0';
            mysql_free_result(result);
            return 0;
        }
        cur_id = atoi(parent_id);
        mysql_free_result(result);
        continue;
    }
}

/*int ls(my_stack *pstack, const char *path_input){
    MYSQL *db = mysql_init(NULL);
    char *host = "localhost";
    char *user = "root";
    char *password = "root";
    char *database = "wangpan_v3";
    MYSQL *ret = mysql_real_connect(db, host, user, password, database, 0, NULL, 0);
    if(ret == NULL){
        printf("Error:%s\n", mysql_error(db));
        exit(0);
    }
    int cur_id = pstack->stack_top->id;
    //char cwd[1024];
    //pwd(pstack, cwd, sizeof(cwd));
    char sql[256];
    if(arg == NULL){
        snprintf(sql, sizeof(sql), "select parent_id, type, path from virtual_directory where id = %d", cur_id);
         
    }    
}*/
